package com.kakaxi.mybilibili.module.video.view;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.kakaxi.mybilibili.R;
import com.kakaxi.mybilibili.adapter.VideoCommentRecyAdapter;
import com.kakaxi.mybilibili.base.BaseFragment;
import com.kakaxi.mybilibili.bean.VideoBean;
import com.kakaxi.mybilibili.bean.VideoCommentBean;
import com.kakaxi.mybilibili.bean.VideoCommentReplyItemBean;
import com.kakaxi.mybilibili.bean.VideoIntroBean;
import com.kakaxi.mybilibili.module.video.presenter.IVideoPresenter;
import com.kakaxi.mybilibili.module.video.presenter.VideoPresenter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 视频评论碎片类
 * Created by kakaxi on 16/11/20.
 */

public class VideoCommentFragment extends BaseFragment implements IVideoView {

    /** 列表 **/
    @BindView(R.id.recv_video_comment_list)
    RecyclerView mRecyclerView;

    /** 控制层 **/
    private IVideoPresenter mPresenter;

    /** 适配器 **/
    private VideoCommentRecyAdapter mAdapter;

    /** 视频a编号 **/
    private String mAid;
    /** 评论的页数 **/
    private int mCommentPn;

    public static VideoCommentFragment newInstance(String aid) {
        VideoCommentFragment videoCommentFragment = new VideoCommentFragment();
        Bundle bundle = new Bundle();
        bundle.putString(VideoActivity.EXTRA_VIDEO_AID, aid);
        videoCommentFragment.setArguments(bundle);
        return videoCommentFragment;
    }

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_video_comment;
    }

    @Override
    protected void setupViews(ViewGroup viewGroup) {
        ButterKnife.bind(this, viewGroup);
    }

    @Override
    protected void initData() {
        mPresenter = new VideoPresenter(this);

        mAid = getArguments().getString(VideoActivity.EXTRA_VIDEO_AID);
        if (mAid != null) {
            initCommentData();
        }
    }

    /**
     * 统一请求
     */
    private void initCommentData() {
        mPresenter.initVideoCommentData(mAid, mCommentPn + 1);
    }

    @Override
    public void showVideoCommentData(VideoCommentBean videoCommentBean) {
        if (videoCommentBean != null) {
            mCommentPn += 1;
            List<VideoCommentReplyItemBean> repliesList = videoCommentBean.getReplies();
            if (mAdapter == null) {
                mAdapter = new VideoCommentRecyAdapter(repliesList);
                mRecyclerView.setAdapter(mAdapter);
                mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
                    @Override
                    public void onLoadMoreRequested() {
                        initCommentData();
                    }
                });
            } else {
                mAdapter.addData(repliesList);
                mAdapter.loadMoreComplete();
            }
            // 如果获取的列表小于请求的大小, 说明已经没有数据了, 设置不可再加载
            if (repliesList.size() < videoCommentBean.getPage().getSize()) {
                mAdapter.loadMoreEnd();
            }
        }
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showEmptyView() {

    }

    @Override
    public void showMessage(String message) {
        toastHandler(message);
    }

    @Override
    public void showVideoData(VideoIntroBean videoIntroBean, VideoBean videoBean) {
    }

    @Override
    public void showVideoData(List<String> videoUrlList) {
    }

    @Override
    public void showVideoIntroData(VideoIntroBean videoIntroBean) {
    }
}
